package br.gerenciadorAudio;

import java.util.List;

import br.gerenciadorAudio.BLL.Constant;
import br.gerenciadorAudio.BLL.RegraNegocio;
import br.gerenciadorAudio.Model.Profile;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListProfileActivity extends Activity{

	private RegraNegocio rn;
	private OnItemClickListener onItemClickListener;
	private OnItemLongClickListener onItemLongClickListener;
	private ListView lv;
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, 0, 4, "Sair");
		menu.add(0, 1, 3, "Sobre");
		menu.add(0, 2, 1, "Novo Perfil");
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case 0:
				finish();
			break;
		case 1:
				startActivity(new Intent(this, AboutActivity.class));
			break;
		case 2:
				Intent i = new Intent(this,EditProfileActivity.class);
				i.putExtra(Profile.Colunas.Id, "0");
				startActivityForResult(i,0);
				this.finish();
			break;
		}
		return true;
	}
	
	public void CarregaLista() throws Exception
	{
		lv = (ListView)findViewById(R.id.lvPerfil);
		
		rn = new RegraNegocio(getApplicationContext());    
        List<Profile> p = rn.ListarPerfis();
        
        for (Profile profile : p) {
			if(profile.Default)
				profile.Name += " (Default)";
		}
        
        if(!(p.size() > 0))
        {
        	Toast.makeText(getApplicationContext(),
					"Nenhum perfil cadastrado. Clique em \"Menu\" para adicionar um novo",
					Toast.LENGTH_LONG).show();
        	return;
        }
        
        ArrayAdapter<Profile> adapter = new ArrayAdapter<Profile>(this,android.R.layout.simple_list_item_single_choice,p);
        lv.setItemsCanFocus(false);
        lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        lv.setAdapter(adapter);
	}
	
	public void SetFlagListView() 
	{
		lv = (ListView)findViewById(R.id.lvPerfil);
		rn = new RegraNegocio(getApplicationContext());
		
		for (int i = 0; i < lv.getCount(); i++) {
			int id = ((Profile)lv.getItemAtPosition(i)).Id;
			Profile p;
			try {
				p = rn.CarregaPerfilPorId(id);
				if(p.Active)
					lv.setItemChecked(i, true);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				Log.e(Constant.categoryLog,"Loc: "  + e.getLocalizedMessage() + " Msg:" + e.getMessage());
			}
		}
	}
	
	@Override	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rn = new RegraNegocio(getApplicationContext());
        rn.CriarBancoDados(getApplicationContext());
		setContentView(R.layout.listprofile);
        
        try
        {
        	rn.SetAlarm(getApplicationContext());
        	CarregaLista();	 
        	lv = (ListView)findViewById(br.gerenciadorAudio.R.id.lvPerfil);
        	
        	onItemLongClickListener = new OnItemLongClickListener() {

				public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					ListView l = (ListView)arg0;
					String idPerfil = String.valueOf(((Profile) l.getItemAtPosition(arg2)).Id);
					AbrirPerfil(idPerfil);
					return false;
				}
			};
        	
			onItemClickListener = new OnItemClickListener() {

				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					ListView l = (ListView)arg0;
					String idPerfil = String.valueOf(((Profile) l.getItemAtPosition(arg2)).Id);
					try 
					{
						rn.SetVolume(getApplicationContext(),idPerfil);
					} catch (Exception e) {
						Log.e(Constant.categoryLog,"Loc: "  + e.getLocalizedMessage() + " Msg:" + e.getMessage());
					}					
				}
			};
			
			lv.setOnItemClickListener(onItemClickListener);
			lv.setOnItemLongClickListener(onItemLongClickListener);
			SetFlagListView();
	               
        }
        catch (Exception e) {
			// TODO: handle exception
        	Log.e(Constant.categoryLog,"Loc: "  + e.getLocalizedMessage() + " Msg:" + e.getMessage());
		}
	}

	protected void AbrirPerfil(String idPerfil) {
		// TODO Auto-generated method stub
		Intent i = new Intent(this, EditProfileActivity.class);
		i.putExtra(Profile.Colunas.Id, idPerfil);
		startActivityForResult(i, 0);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    super.onActivityResult(requestCode, resultCode, data);
	    try 
	    {
			CarregaLista();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Log.e(Constant.categoryLog,"Loc: "  + e.getLocalizedMessage() + " Msg:" + e.getMessage());
		}
	}
}
